<template>
  <el-dialog
    :title="!dataForm.jobId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
      <el-form-item label="任务名称" prop="taskName">
        <el-input v-model="dataForm.taskName" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="任务描述" prop="taskDescription">
        <el-input v-model="dataForm.taskDescription" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="表名称" prop="tableName">
        <el-select v-model="dataForm.tableName" placeholder="请选择">
          <el-option
            v-for="(item,index) in options"
            :key="index"
            :label="item.name"
            :value="item.tableName">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="方法名称" prop="methodName">
        <el-input v-model="dataForm.methodName" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="bean名称" prop="beanName">
        <el-input v-model="dataForm.beanName" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="参数" prop="params">
        <el-input v-model="dataForm.params" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="cron表达式" prop="cronExpression">
        <el-input v-model="dataForm.cronExpression" placeholder=""></el-input>
        <p style="padding:0;margin:0">
          参考地址：<a href="https://qqe2.com/cron" target="_blank" rel="noopener noreferrer"> https://qqe2.com/cron</a>
        </p>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder=""></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import request from '@/utils/request'
  export default {
    data () {
      return {
        visible: false,
        options:[],
        dataForm: {
          jobId:null,
          beanName: 'ApiDataTask',
          params: '',
          cronExpression: '',
          remark: '',
          taskName:"",
          taskDescription:"",
          tableName:"",
          methodName:"runTask",
          status: 0
        },
        dataRule: {
          beanName: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          // cronExpression: [
          //   { required: true, message: 'cron表达式不能为空', trigger: 'blur' }
          // ]
        }
      }
    },
    mounted(){
      this.$nextTick(()=>{
        this.getSelectDataList()
      })
    },
    methods: {
      reset(){
        this.dataForm = {
          jobId:null,
          beanName: 'ApiDataTask',
          params: '',
          cronExpression: '',
          remark: '',
          taskName:"",
          taskDescription:"",
          tableName:"",
          methodName:"runTask",
          status: 0
        }
      },
      close(){
        this.visible = false
      },
      getSelectDataList(){
        request({
          url: '/ht-datadevelop/table/manage/getListToConfig',
          method: 'get',
          params: {}
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.options = data.data
          }
        })
      },
      init (row) {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.dataForm = row
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            request({
              url:`/ht-datadevelop/sys/schedule/${!this.dataForm.jobId ? 'save' : 'update'}`,
              method: 'post',
              data: {
                'jobId': this.dataForm.jobId || undefined,
                'beanName': this.dataForm.beanName,
                'params': this.dataForm.params,
                'cronExpression': this.dataForm.cronExpression,
                'remark': this.dataForm.remark,
                'status': !this.dataForm.jobId ? undefined : this.dataForm.status,
                'taskName':this.dataForm.taskName,
                'taskDescription':this.dataForm.taskDescription,
                'tableName':this.dataForm.tableName,
                'methodName':this.dataForm.methodName
              }
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>



<!-- <style lang="scss" scoped>
/deep/.el-select{
  width:100% !important;
} -->

  .el-form{
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    .el-form-item{
      width:50%;
    }
  }</style>